package com.example.downloaddemo;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiewei on 15-2-4.
 */
public class ProgressAdapter extends BaseAdapter {
	private List<String> list;
	private DownLoader loader;//下载器
	private LoadInfo info;
	private Handler handler;
	private String path;
	private HashMap<String, DownLoader> loaders;
	private HashMap<String, View> views;
	private HashMap<String, ProgressBar> bars;

	public ProgressAdapter(List<String> urllist, Handler handler, HashMap<String, DownLoader> loaders, HashMap<String, View> views, HashMap<String, ProgressBar> bars) {
		this.list = urllist;
		this.handler = handler;
		this.loaders = loaders;
		path = Environment.getExternalStorageDirectory().getPath() + "/downloaddemo";
		this.bars = bars;
		this.views =views;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, final ViewGroup parent) {
		final String url = list.get(position);
		if(views.containsKey(url)){
			return views.get(url);
		}
		TextView tv_name;
		final ProgressBar progress;
		final Button down;
		Button stop;
		convertView = View.inflate(parent.getContext(), R.layout.item, null);
		tv_name = (TextView) convertView.findViewById(R.id.tv_name);
		progress = (ProgressBar) convertView.findViewById(R.id.progress);
		down = (Button) convertView.findViewById(R.id.down);
		//stop = (Button) convertView.findViewById(R.id.stop);



		tv_name.setText(url.substring(url.lastIndexOf("/") + 1));

		if (loaders.get(url) == null || loader.state != Constants.DOWNLOADING) {
			down.setText("下载");
		} else {
			down.setText("暂停");
		}
		final View finalConvertView = convertView;
		down.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				loader = loaders.get(list.get(position));
				if (loader == null) {
					loader = new DownLoader(list.get(position), path, 1, parent.getContext(), handler);
					loaders.put(list.get(position), loader);
				}

			//	progress.setBackgroundColor(0xff111111);
				if ("暂停".equals(down.getText())) {
					loader.pause();
					down.setText("下载");
				} else {
					bars.put(url,progress);
					down.setText("暂停");
					new Thread() {
						@Override
						public void run() {
							Log.i("msg", "loader:" + loader);
							info = loader.getLoadInfo();
							Message msg = new Message();
							msg.what = 2;
							msg.obj = info;
							handler.sendMessage(msg);
							loader.download();

						}
					}.start();
				}
				views.put(url, finalConvertView);
			}
		});
		return convertView;
	}


}
